// <editor-fold defaultstate="collapsed" desc="GNU GPLv3 Header">
/*
 * File:   ChunkDataNonPCMByte.java
 *
 * Copyright (C) 2013 Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * Copyright (C) 2013 David Megias Jimenez <dmegias@uoc.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// </editor-fold>
package uoc.libwave;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Class for storing the data chunk of a Non-PCM U8 audio file in Non-PCM U8 format
 * and, for reading and writing its content in Data I/O Streams.
 *
 * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * @version 1.0
 * @since 2013-08-31
 */
final class ChunkDataNonPCMByte extends ChunkDataNonPCM<Byte> {

    /**
     * Samples data
     */
    private byte[][] lSamples;

    /**
     *
     * @since 2013-08-31
     */
    ChunkDataNonPCMByte() {
    }

    ChunkDataNonPCMByte(String DataChunkID, int DataChunkSize, short wBitsPerSample,
            short wChannels, DataInputStream dis) throws IOException {
        super(DataChunkID, DataChunkSize);
        nChannels = wChannels;
        nSamples = ckSize / nChannels;
        readSamples(dis);
    }

    /**
     *
     * @param wFormatTag
     * @param sample_data
     * @param lenght
     * @param wChannel
     * @since 2013-08-31
     */
    ChunkDataNonPCMByte(short wFormatTag, byte[][] sample_data, int lenght,
            short wChannel) {
        super(lenght * wChannel);
        nChannels = wChannel;
        nSamples = lenght;
        lSamples = new byte[wChannel][lenght];
        for (int i = 0; i < wChannel; i++) {
            for (int j = 0; j < lenght; j++) {
                lSamples[i][j] = sample_data[i][j];
            }
        }
    }

    /**
     *
     * @return
     * @since 2013-08-31
     */
    @Override
    Byte getMaxSample() {
        byte result = 0;
        byte temp = 0;
        for (int i = 0; i < nSamples; i++) {
            for (int j = 0; j < nChannels; j++) {
                temp = (byte) Math.abs(lSamples[j][i]);
                if (temp > result) {
                    result = temp;
                }
            }
        }
        return result;
    }

    /**
     *
     * @param dis
     * @throws IOException
     * @since 2013-08-31
     */
    @Override
    void readSamples(DataInputStream dis) throws IOException {
        lSamples = new byte[nChannels][nSamples];
        for (int i = 0; i < nSamples; i++) {
            for (int j = 0; j < nChannels; j++) {
                lSamples[j][i] = readByteLE(dis);
            }
        }
    }

    /**
     *
     * @param dos
     * @throws IOException
     * @since 2013-08-31
     */
    @Override
    void save(DataOutputStream dos) throws IOException {
        writeCkID(dos);
        writeCkSize(dos);
        writeSamples(dos);
    }

    /**
     *
     * @param wChannel
     * @param dwPosition
     * @return
     * @since 2013-08-31
     */
    @Override
    Byte getSample(short wChannel, int dwPosition) {
        return lSamples[wChannel - 1][dwPosition];
    }

    /**
     *
     * @param wChannel
     * @param dwPosition
     * @param wSample
     * @since 2013-08-31
     */
    @Override
    void setSample(short wChannel, int dwPosition, Byte wSample) {
        lSamples[wChannel - 1][dwPosition] = wSample;
    }

    /**
     *
     * @param start
     * @param size
     * @param _Channel
     * @param lframe
     * @since 2013-08-31
     */
    @Override
    void getFrame(int start, short size, short _Channel, List<Byte> lframe) {
        lframe.clear();
        for (int i = 0; i < size; i++) {
            lframe.add(getSample(_Channel, start + i));
        }
    }

    /**
     *
     * @param start
     * @param lframe
     * @param _Channel
     * @since 2013-08-31
     */
    @Override
    void setFrame(int start, List<Byte> lframe, short _Channel) {
        for (int i = 0; i < lframe.size(); i++) {
            setSample(_Channel, start + i, lframe.get(i));
        }
    }

    /**
     *
     * @param dos
     * @throws IOException
     * @since 2013-08-31
     */
    @Override
    void writeSamples(DataOutputStream dos) throws IOException {
        for (int i = 0; i < nSamples; i++) {
            for (int j = 0; j < nChannels; j++) {
                writeByteLE(lSamples[j][i], dos);
            }
        }
    }
}
